/**//**//**//**//**//**//*  
**    ==================================================================================================  
**    类名：F.highlight  
**    功能：语法高亮  
**    示例：  
    ---------------------------------------------------------------------------------------------------  
  
        	用法1：var html = F.highlight(strCode);         
            用法2：F("box").highlight(strCode); 
  
    ---------------------------------------------------------------------------------------------------  
**    作者：Li
**    邮件：61114579@qq.com  
**    日期：2011-8-26  
**    ==================================================================================================  
**/

~function(F){
	//语法高亮类
	F.highlight = F.Fun({
		init : function(codetxt, lang, skinName){
			if(!codetxt) return "";
			
			lang = lang || "html";
			skinName = skinName || "dreamweaver";
			this.skin = this.skins[skinName];
			var dom = F("$1stjs_highlight");
			if(!dom){
				var a = [], o, langs={js:1,css:1,html:1};
				for(var lan in langs){
					o = this.skin[lan];
					for(p in o){
						a.push(F.mix(".$0$ .$1$ .$2$ { $3$ }", [skinName,lan,p,o[p]]));
					}
				}
				F.css(a).attr("id", "$1stjs_highlight");
			}
			return '<span class="'+skinName+'">'+this.parsecode(codetxt, lang)+'</span>';
		},
		//获取代码片段
		getSplinter : function(codetxt, dword){
			var a = [];
			var oldword = false;
			codetxt.replace(/[\s\S]/g, function(s){
				var isword = dword.indexOf(s)==-1;
				isword && oldword ? a[a.length-1]+=s : a.push(s);
				oldword = isword;
			});
			return a;			
		},		
		//代码片段解析
		parsecode : function(codetxt, lang){
			switch(lang){
				case "js" : return this.js_parser(codetxt);
				case "css" : return this.css_parser(codetxt);				
				case "html" : return this.html_parser(codetxt);				
			}
		},
		//JS解析
		js_parser : function(codetxt){
			if(!codetxt) return "";
			var a = this.getSplinter(codetxt, "　 ,?!:;\\/<>(){}[]\"'\r\n\t=+-|*%^&");
			//alert(a);
			
			var keys = this.keys.js;
			var skin = this.skin.js;
			var len = a.length;
			var lastIdx = len - 1;
			var quote_opened = false;		//引用标记
			var quote_tag = "";				//引用标记类型
			var comment_opened = false;		//单行注释标记			
			var comment_type = "";			//注释类型 SL=单行, ML=多行		
			var re_opened = false;			//正则标记
			var rebra_opened = false;		//正则里的中括号
			//var line_num = 1;				//行号
			
			var html = [], i=0, s, pre=[], ispre;
			while(i<len){
				ispre = true;
				s = a[i];
				
				//处理空格
				if(s==" "){
					s = "&nbsp;";
				}
				
				//处理换行
				else if(s=="\n"){
					if(comment_opened && comment_type=="SL"){
						ispre = false;
						comment_opened = false;
						s = "</span><br/>";
					}else{
						s = "<br/>";
					}
					//line_num++;
				}
				
				//处理TAB缩进
				else if(s=="\t"){
					s = "&nbsp;&nbsp;&nbsp;&nbsp;";
				}
				
				//处理标签
				else if(s=="<"||s==">"){
					s = s.replace("<","&lt;").replace(">","&gt;");
				}
				
				//处理转义
				else if(s=="\\"){
					if(!comment_opened){
					//if(a[i+1]>=32&&a[i+1]<=127){
						if(i<lastIdx){
							s = "\\" + a[++i];
						}
					//}
					}
				}
				
				//处理引号（引号前不能为转义字符）			
				else if(s=="'" || s=='"'){
					if(!comment_opened && !re_opened){
						//打开
						if(!quote_opened){
							quote_opened = true;
							quote_tag = s;
							s = '<span class="string">' + s;
						//关闭
						}else if(quote_tag==s)	{
							s = s + '</span>';
							quote_opened = false;
							quote_tag = "";
						}
					}
				}
				
				//处理注释和正则
				else if(s=="/"){
					//处理多行注释结尾
					if(comment_opened && comment_type=="ML" && i>0 && a[i-1]=="*"){
						s = '/</span>';
						ispre = false;
						comment_opened = false;
					}else if(!comment_opened && !quote_opened){
						//处理单行注释开头
						if(i<lastIdx && a[i+1]=="/"){
							comment_opened = true;
							comment_type = "SL";
							s = '<span class="comment">//';
							++i;
							
						//处理多行注释开头
						}else if(i<lastIdx && a[i+1]=="*"){
							comment_opened = true;
							comment_type = "ML";
							s = '<span class="comment">/*';
							++i;
							
						//处理正则	
						}else{
						//chkre start

							//判断前半部分是否有运算符和括号，以区分跟除号的区别
							var isre = pre.length==0 || /[*/%<=>&|!~^?:;,([{}]|^(?:typeof|in|case|void|throw|return)$/.test(pre[0]) || /^(?:\+|\+\+\+|\-|\-\-\-)$/.test(pre.join(""));
							//判断后半部分是否有另一个斜杠
							if(isre){
								isre = false;
								var isbra = false;
								var k = i;
								var t = ["/"];
								while(k<=lastIdx && a[k]!="\n"){
									t.push(a[++k]);
									//处理正则表达式中的转义
									if(a[k]=="\\"){
										k<lastIdx && t.push(a[++k]);
									}							
									//处理正则表达式中的中括号
									else if(a[k]=="[" || a[k]=="]"){
										isbra = a[k]=="[";
									}
									//处理正则结尾
									else if(a[k]=="/" && isbra==false){
										while(k<lastIdx && /[img]/.test(a[k+1])){
											t.push(a[++k]);
										};
										s = '<span class="regexp">'+t.join("").replace("<","&lt;").replace(">","&gt;")+'</span>';
										i = k;
										isre = true;
										break;
									}
								}
							}
							if(!isre) s='<span class="operator">'+s+'</span>';
							
						//chkre end
						}
					}
				}
				
				//处理关键字颜色代码
				else if(!comment_opened && !quote_opened){
					for(var k in keys){
						if(keys[k].test(s)){
							s = '<span class="'+k+'">'+s+'</span>'
							break;
						}
					}
				}
				
				if(!comment_opened && !quote_opened && ispre && /\S/.test(a[i])){
					pre.unshift(a[i]);
					pre.length>3 && pre.pop();
				}
				html.push(s);
				++i;
			};
			return '<span class="js">'+html.join("")+'</span>';
		},
		//CSS解析
		css_parser : function(codetxt){
			var a = this.getSplinter(codetxt, "　 {:;}<>\r\n\t/*");
			//alert(a);
			var skin = this.skin.css;
			var len = a.length;
			var lastIdx = len - 1;
			var comment_opened = false;		//注释
			var block_opened = false;		//语句块
			var colon_opened = false;		//冒号
			var html = [], i=0, s;
			while(i<len){
				s = a[i];

				//处理空格
				if(s==" "){
					s = "&nbsp;";
				}
				//处理换行
				else if(s=="\n"){
					s = "<br/>";
				}
				//处理TAB缩进
				else if(s=="\t"){
					s = "&nbsp;&nbsp;&nbsp;&nbsp;";
				}
				//处理import
				else if(s=="!" && i<lastIdx && a[i+1]=="import"){
					s = '<span class="_import">'+s+'</span>';
				}
				//处理important
				else if(s=="@" && i<lastIdx && a[i+1]=="important"){
					s = '<span class="important">'+s+'</span>';
				}					
				//处理media
				else if(s=="@" && i<lastIdx && a[i+1]=="media"){
					s = '<span class="media">'+s+'</span>';
				}
				
				//处理标签
				else if(s=="<"||s==">"){
					s = s.replace("<","&lt;").replace(">","&gt;");
				}else if(!comment_opened){
					//处理花括号
					if(s=="{"||s=="}"){
						block_opened = s=="{";
					//处理冒号
					}else if(s==":"){
						if(block_opened){
							colon_opened = true;
						}else{
							s = '<span class="selector">'+s+'</span>';
						}
					//处理分号
					}else if(s==";"){
						colon_opened = false;
					//处理注释开头
					}else if(s=="/" && i<lastIdx && a[i+1]=="*"){
						comment_opened = true;
						s = '<span class="comment">/*';
						++i;					
					//高亮色					
					}else{
						if(!block_opened){
							s = '<span class="selector">'+s+'</span>';
						}else{
							if(!colon_opened){
								s = '<span class="property">'+s+'</span>';
							}else{
								s = '<span class="value">'+s+'</span>';
							}
						}
					}
				//处理注释结尾
				}else if(comment_opened && s=="/" && i>0 && a[i-1]=="*"){
					s = '/</span>';						
					comment_opened = false;
				}
				
				html.push(s);
				++i;
			};
			return '<span class="css">'+html.join("")+'</span>';
		},	
		//HTML解析
		html_parser : function(codetxt){
			var html = [];
			var re = /(<(?:script|style)[\s\S]*?>)([\s\S]*?)(<\/(script|style)>)/i;
			while(re.test(codetxt)){
				var s1=RegExp.$1, s2=RegExp.$2, s3=RegExp.$3, s4=RegExp.$4.toLowerCase(), s=RegExp.leftContext+s1;	
				codetxt = s3 + RegExp.rightContext;
				var t = s4=="script" ? this.js_parser(s2) : this.css_parser(s2);
				html.push( this.htmlcode_parser(s)+t );
			}
			html.push( this.htmlcode_parser(codetxt) );
			return html.join("");
		},
		//HTML代码解析
		htmlcode_parser : function(codetxt){
			var a = this.getSplinter(codetxt, "　 <>\r\n\t/\"'");
			//alert(a);

			var keys = this.keys.html;
			var skin = this.skin.html;
			var len = a.length;
			var lastIdx = len - 1;
			var comment_opened = false;		//注释
			var quote_opened = false;		//字符串
			var block_opened = false;		//语句块
			var colon_opened = false;		//冒号
			var color = "";
			var html = [], i=0, s;
			while(i<len){
				s = a[i];

				//处理空格
				if(s==" "){
					s = "&nbsp;";
				}
				//处理换行
				else if(s=="\n"){
					s = "<br/>";
				}
				//处理TAB缩进
				else if(s=="\t"){
					s = "&nbsp;&nbsp;&nbsp;&nbsp;";
				}
				
				//处理左尖括号
				else if(s=="<"){
					s = "&lt;";
					
					if(!comment_opened && !quote_opened && i<lastIdx){
						var t = a[i+1];
						//处理注释开始
						if(t=="!--"){
							comment_opened = true;
							s = '<span class="comment">' + s + a[++i];
						}
						
						//处理关键字颜色代码
						else if(t=="/" && i<lastIdx-2 && a[i+3]==">"){
							t = a[i+2];
							for(var k in keys){
								if(keys[k].test(t)){
									s = '<span class="'+k+'">&lt;/'+t+'&gt;</span>';
									i += 3;
									break;
								}
							}							
						}
						
						else{
							for(var k in keys){
								if(keys[k].test(t)){
									s = '<span class="'+k+'">&lt;'+a[++i];//+'&gt;</span>';
									break;
								}
							}							
						}
					}
				}
				
				//处理右尖括号
				else if(s==">"){
					s = "&gt;";
					if(comment_opened && i>0 && a[i-1]=="--"){
						comment_opened = false;
						s = '&gt;</span>';
					}else if(!comment_opened){
						s = '&gt;</span>';
					}
				}
				
				//处理引号
				else if(s=="'" || s=='"'){
					if(!comment_opened){
						//打开
						if(!quote_opened){
							quote_opened = true;
							quote_tag = s;
							s = '<span class="attribute_value">' + s;
						//关闭
						}else if(quote_tag==s)	{
							s = s + '</span>';
							quote_opened = false;
							quote_tag = "";
						}
					}
				}
				html.push(s);
				++i;
			}
			return '<span class="html">'+html.join("")+'</span>';
		},
		//关键字正则
		keys : {
			//JS关键字
			js : {
				bracket : /[{}[\]()]/,				//括号
				client_keywords : /^(?:window|document|body|alert|open|setTimeout|setInterval|location|execScript)$/,		//客户端关键字
				function_keyword : /^function$/,					//函数关键字			
				native_keywords : /^(?:String|Number|Array|Boolean|RegExp|Math|Date|Function|Object|arguments|Error|eval|parseInt|parseFloat|escape|unescape|decodeURI|decodeURIComponent|encodeURI|encodeURIComponent|isNaN|isFinite|typeof)$/,				//内建对象及global函数
				number : /^(?:(0x[0-9a-fA-F]+|(?:\d+\.\d*|\d*\.\d+|\d+)(?:[eE][+\-]?\d+)?))$/,				//数字 //还有Infinity
				operator : /[+\-*/%<=>&\|!~^?:]/,		//运算符				
				reserved_keywords : /^(?:return|if|else|switch|case|break|default|for|in|do|while|continue|try|catch|finally|throw|void|with|new|delete|var|this|true|false|null|class)$/		//保留关键字
			},
			//CSS关键字
			css : {
				important : /!important/,
				media : /@media/,
				_import : /@import/
			},
			//HTML关键字
			html : {
				anchor_tags : /^(a)$/i,
				form_tags : /^(form|input|textarea|button|select|option)$/i,
				images_tags : /^(img)$/i,
				object_tags : /^(object|embed)$/i,
				//other_tags : //,
				script_tags : /^(script)$/i,
				//special_characters : //,
				style_tags : /^(link|style)$/i,
				table_tags : /^(table|tr|td|caption)$/i,
				tags : /^(!doctype|\?import|html|head|meta|title|body|div|span|p|ul|ol|li|dl|dt|dd|br|em|u|i|b|s|font|iframe|frame|frameset|h1|h2|h3|h4|h5|h6)$/i
			}
		},
		//皮肤配置
		skins : {
			//adobe dreamweaver
			dreamweaver : {
				js : {
					bracket : "color:#009; font-weight:bold;",			//括号
					client_keywords : "color:#909;",					//客户端关键字
					comment : "color:#999;",							//注释
					default_text : "color:#000;",						//默认字符
					function_keyword : "color:#000; font-weight:bold;",	//函数关键字
					identifier : "color:#000;",							//标记符
					native_keywords : "color:#099;",					//内建对象
					number : "color:#f00;",								//数字
					operator : "color:#00f;",							//运算符
					regexp : "color:#060;",								//正则表达式
					reserved_keywords : "color:#009; font-weight:bold;",//保留关键字
					string : "color:#00f;"								//字符串
				},
				css : {
					_import : "color:#099; font-weight:bold;",			//import
					media : "color:#900; font-weight:bold;",			//@media
					comment : "color:#999;",							//注释
					important : "color:#f00; font-weight:bold;",		//!important
					property : "color:#009;",							//属性
					selector : "color:#f0f;",							//选择器
					string : "color:#060;",								//字符串	
					value : "color:#00f;"								//属性值				
				},
				html : {
					anchor_tags : "color:#060;",						//瞄标签
					attribute_value : "color:#00f;",					//属性值
					comment : "color:#999;",							//注释
					form_tags : "color:#f90;",							//表单标签
					image_tags : "color:#909;",							//图片标签
					number : "color:#00f;",								//数字
					object_tags : "color:#900;",						//多媒体标签
					other_tags : "color:#009;",							//其他标签
					script_tags : "color:#900;",						//脚本标签
					special_characters : "color:#000;font-weight:bold;",//特殊字符
					style_tags : "color:#909;",							//样式标签
					table_tags : "color:#099;",							//表格标签
					tags : "color:#009;",								//标签
					text : "color:#000;"								//文本
				},
				bgcolor : "#fff"
			},
			other : {
				js : {
					
				},
				css : {
					
				},
				html : {
					
				}
			}
		}
	});
}($1stjs);